﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using InternalManagementSystem.DAO;
using InternalManagementSystem.DAO.Models;
using System.Data;
using System.Transactions;
using System.Configuration;
using System.Web.Configuration;

/// <summary>
///CompanyBLL 的摘要说明
/// </summary>
public class CompanyBLL
{
    Permission permission = null;

    public string MainCompanyName { get; set; }

    public CompanyBLL()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //

    }

    public CompanyBLL(Permission permission)
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
        this.permission = permission;
    }

    #region 公司部门组织进行自由增加和删除


    /// <summary>
    /// 获取公司
    /// </summary>
    /// <param name="Companyid"></param>
    /// <returns></returns>
    public Company GetCompany(int companyid)
    {
        CompanyDAO dao = new CompanyDAO();
        return dao.LoadCompany(companyid);
    }

    /// <summary>
    /// 获取公司列表,根据角色控制结果
    /// </summary>
    /// <returns></returns>
    public IList<Company> GetAllCompany()
    {
        CompanyDAO dao = new CompanyDAO();

        return dao.LoadAllCompanys().Cast<Company>().OrderBy(o => o.CompanyID).ToList();

    }

    public DataSet GetCompanyAssigned(int leaderid)
    {
        DBHelper helper = new DBHelper();
        string sql = string.Format("select * from v_CompanyAssigned where LeaderID = {0} order by CompanyID", leaderid);
        return helper.ExecuteQuerySQL(sql);
    }

    public DataSet GetCompanyAssignedByCompanyID(int companyid)
    {
        DBHelper helper = new DBHelper();
        string sql = string.Format("select * from v_CompanyAssigned where CompanyID = {0} order by CompanyID", companyid);
        return helper.ExecuteQuerySQL(sql);
    }

    /// <summary>
    /// 新增公司
    /// </summary>
    /// <param name="Company"></param>
    /// <returns></returns>
    public bool AddCompany(Company Company)
    {
        bool result = false;
        if (permission.IsCompanyEdit)
        {
            try
            {
                CompanyDAO dao = new CompanyDAO();

                string query = "CompanyName = '" + Company.CompanyName + "'";

                if (dao.LoadAllCompanys(query, "").Count == 0)
                {
                    dao.InsertCompany(Company);
                    result = true;
                }
            }
            catch
            {

            }
        }

        return result;
    }

    /// <summary>
    /// 更新公司
    /// </summary>
    /// <param name="Company"></param>
    /// <returns></returns>
    public bool UpdateCompany(Company Company)
    {
        bool result = false;
        if (permission.IsCompanyEdit)
        {
            try
            {
                CompanyDAO dao = new CompanyDAO();

                if (Company.CompanyName == MainCompanyName)
                    return false;

                dao.UpdateCompany(Company);

                result = true;
            }
            catch
            {

            }
        }

        return result;
    }

    /// <summary>
    /// 删除公司
    /// </summary>
    /// <param name="Companyid"></param>
    /// <returns></returns>
    public bool DeleteCompany(int Companyid)
    {
        bool result = false;
        if (permission.IsCompanyEdit)
        {
            try
            {
                CompanyDAO dao = new CompanyDAO();
                Company company = dao.LoadCompany(Companyid);
                if (company.CompanyName == MainCompanyName)
                    return false;

                StaffDAO userdao = new StaffDAO();
                UOCDAO uocdao = new UOCDAO();

                using (var transactionScope = new TransactionScope())
                {
                    try
                    {
                        ////删除相关员工
                        UserCollection usercol = userdao.LoadAllUsers("CompanyID = " + Companyid.ToString(), "");
                        foreach (Staff user in usercol)
                        {
                            //删除员工相关uoc
                            UOCCollection uoccol = uocdao.LoadAllUOCs("userid = " + user.UserID.ToString(), "");
                            foreach (UOC uoc in uoccol)
                            {
                                uocdao.DeleteUOC(uoc.ID);
                            }

                            userdao.DeleteUser(user.UserID);
                        }

                        //删除公司相关uoc
                        UOCCollection cuoccol = uocdao.LoadAllUOCs("Company = " + Companyid.ToString(), "");
                        foreach (UOC uoc in cuoccol)
                        {
                            uocdao.DeleteUOC(uoc.ID);
                        }

                        dao.DeleteCompany(Companyid);

                        
                    }
                    catch
                    {

                    }
                    finally
                    {
                        transactionScope.Complete();
                    }
                }

                result = true;
            }
            catch
            {

            }
        }

        return result;
    }



    #endregion

    /// <summary>
    /// 设定违章违纪记录统计的时间段信息
    /// </summary>
    /// <param name="companyid"></param>
    /// <param name="startTime"></param>
    /// <param name="cycle"></param>
    public void ViolationNoticeTimeEdit(int companyid, DateTime startTime, DateTime endTime)
    {
        if (permission.IsViolationNoticeTimeEdit)
        {
            CompanyDAO dao = new CompanyDAO();
            Company company = dao.LoadCompany(companyid);
            if (company != null)
            {
                company.StatisticalEndDate = endTime;
                company.StatisticalStartDate = startTime;
                dao.UpdateCompany(company);
            }
        }
    }

    /*
    每年最后一天对没有扣分到6分的员工和单位自动重置到12分，
    *  
    * （安全人员才有权限，自动充值的范围可以自行设定，即设定“是否自动重置”的可选框）。
    */
    public void SetAutoRecharge(int companyid, bool isAuto)
    {
        if (permission.IsAutoRechargEdit)
        {
            CompanyDAO dao = new CompanyDAO();
            Company company = dao.LoadCompany(companyid);
            if (isAuto)
                company.IsAutoRecharge = 1;
            else
                company.IsAutoRecharge = 0;

            dao.UpdateCompany(company);
        }
    }

    /*
     到了新周期，都重置
不管扣分没扣分
     */
    public void AutoRecharge(int companyid)
    {

        CompanyDAO dao = new CompanyDAO();
        Company company = dao.LoadCompany(companyid);
        if (company.IsAutoRecharge == 1)
        {
            if (company.CurrentIntegral != 12)
            {
                company.CurrentIntegral = 12;
                dao.UpdateCompany(company);//重置公司
            }

            UserBLL ubll = new UserBLL(this.permission);
            ubll.AutoRechargeInCompany(companyid);//重置公司所有用户
        }

    }


    /// <summary>
    /// 公司就一个没有分类了，比如总分12分，警戒线6分，放在公司属性里就可以了
    /// 是否超出警戒线
    /// </summary>
    /// <param name="usertypeid"></param>
    /// <param name="currentIntegral"></param>
    /// <returns></returns>
    public bool IsOverGuardLine(int currentIntegral, int GuardLine)
    {
        bool result = false;
        if (currentIntegral <= GuardLine)
        {
            result = true;
        }

        return result;
    }

    public void DoAssign(int companyID, int leaderID)
    {
        UOCDAO dao = new UOCDAO();
        //检测
        UOCCollection col = dao.LoadAllUOCs(string.Format("UserID={0} and Company={1}", leaderID, companyID), string.Empty);
        if (col.Count == 0)
        {
            UOC uoc = new UOC();
            uoc.Company = companyID;
            uoc.UserID = leaderID;
            dao.InsertUOC(uoc);
        }

    }
}
